package se19team1e.dao.impl.property;

import java.util.ArrayList;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.CompanyDAO;
import se19team1e.dao.abstrakt.property.PropertyDAO;
import se19team1e.entity.CompanyDTO;
import se19team1e.exception.DataAccessException;

/**
 * Concrete class implementation of the Property DAO, Property DAO is handling
 * company in this implementation.
 * 
 * @author Nasri Bin Najib
 * 
 */
public class CompanyPropertyDAO extends PropertyDAO<CompanyDTO> implements
		CompanyDAO {
	/**
	 * Constructor that calls the super class PropertyDAO to create Company
	 * Property DAO
	 * 
	 * @param jobAdvertProperties
	 */
	public CompanyPropertyDAO(String jobAdvertProperties) {
		super(jobAdvertProperties);
	}

	/**
	 * Convert DTO into properties given the Company DTO object
	 * 
	 * @return true if create Company DTO is successful)
	 * @throws DataAccessException
	 */
	@Override
	public boolean create(CompanyDTO dtoType) throws DataAccessException {

		convertDTOIntoProperties(dtoType);
		persist();
		return true;
	}

	/**
	 * 
	 * Deletes an Company object given its ID
	 * 
	 * @param Id
	 *            of the Company object to be deleted
	 * @return true if delete is successful
	 * @throws DataAccessException
	 * 
	 */
	@Override
	public boolean delete(Object id) throws DataAccessException {

		CompanyDTO companyDTO = retrieveById(id);
		companyDTO.setDeleted(true);
		convertDTOIntoProperties(companyDTO);
		persist();

		return true;
	}

	/**
	 * DAO will retrieve all Company objects in the persistence storage.
	 * 
	 * @return list of Company DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public List<CompanyDTO> retrieveAll() throws DataAccessException {
		List<CompanyDTO> companyDTOList = new ArrayList<CompanyDTO>();
		int lastSequence = getLastSequence();
		CompanyDTO companyDTO = null;
		for (int currentSequence = 1; currentSequence <= lastSequence; currentSequence++) {
			companyDTO = new CompanyDTO();
			populateValuesIntoDTO(String.valueOf(currentSequence), companyDTO);
			companyDTOList.add(companyDTO);

		}
		return companyDTOList;
	}

	/**
	 * DAO will retrieve Company objects in the persistence storage based on the
	 * id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of Company DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public CompanyDTO retrieveById(Object id) throws DataAccessException {

		CompanyDTO companyDTO = new CompanyDTO();
		populateValuesIntoDTO((String) id, companyDTO);
		return companyDTO;
	}

	/**
	 * DAO will update Company objects in the persistence storage based
	 * 
	 * @param AccountDTO
	 *            Company DataTransfer Object
	 * @return list of Company DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public CompanyDTO update(CompanyDTO dto) throws DataAccessException {

		convertDTOIntoProperties(dto);
		persist();
		return retrieveById(dto.getId());
	}

}
